var express = require('express');
var router = express.Router();
var Goodslist = require("../models/goodslist");


//获取商品数据
router.get('/', function(req, res, next) {
    let page = parseInt(req.param("searchNum"));
    let pageSize = 5;
    // let sort = req.param("sort");
    let skip = (page-1)*pageSize;
    let params = {
        shelves:true,
    };

    if (req.param("className") != undefined){
        params.type=req.param("className")
    }
    // if (req.param("goodsName") != undefined){
    //     params.goodName=req.param("goodsName")
    //
    // }

        console.log(params)
    let model = Goodslist.find(params).skip(skip).limit(pageSize);
    // goodsModel.sort({"salePrice" : sort});
    //var model = Goodslist.find();
    Goodslist.count(params, (err, count) => {
        if(err){
            res.json({
                status:"1",
                msg:err.message
            })
        }else {
            model.exec(function (err1, result) {
                if (err1) {
                    res.json({
                        status: "1",
                        msg: err.message
                    })
                } else {
                    res.json({
                        status: "0",
                        msg: "",
                        result: {
                            count: count,
                            list: result
                        }
                    })
                }
            });
        }
    });
});


module.exports = router;